/**
 * 
 */
package org.gas.barapraja.handler.validator;

import org.apache.log4j.Logger;
import org.gas.barapraja.db.dao.AccountDao;
import org.gas.barapraja.db.entity.Account;
import org.gas.barapraja.db.entity.Token;
import org.gas.barapraja.handler.GasValidatorAdapter;
import org.gas.barapraja.util.GasApplicationContext;

import com.logica.smpp.pdu.SubmitMultiSM;

/**
 * @author gilang
 *
 */
public class MultiMessageValidator extends GasValidatorAdapter {
	
	private static final Logger logger=Logger.getLogger(MultiMessageValidator.class);
	
	private int numOfMsg=0;
	
	private AccountDao accountDao;
	
	public MultiMessageValidator() {
		accountDao=GasApplicationContext.getAccountDao();
	}

	/* (non-Javadoc)
	 * @see org.gas.barapraja.handler.GasValidatorAdapter#validate(com.logica.smpp.pdu.SubmitMultiSM)
	 */
	@Override
	public int validate(SubmitMultiSM multiSM) {
		//ParserFactory factory=ParserUtility.getFactory();
		Account account=accountDao.findByNumber(multiSM.getSourceAddr().getAddress());
		Token token=account.getToken();
		int code=0;
		if(token!=null){
			int sumOfToken=token.getTokenValue();
			if(multiSM.getNumberOfDests()>sumOfToken){
				logger.debug("token is not enough to send multiple messages, token: "+sumOfToken+
						" msg(s) to send: "+multiSM.getNumberOfDests());
				code=TOKEN_ISNT_OK;
			}else{
				numOfMsg=sumOfToken-multiSM.getNumberOfDests();
				logger.debug("ready to send multiple message. number of message to send: "+numOfMsg);
				code=TOKEN_OK;
			}
		}else{
			code=TOKEN_ISNT_OK;
		}
		return code;
	}

	/**
	 * @return the numOfMsg
	 */
	public int getNumOfMsg() {
		return numOfMsg;
	}
	
	

}
